package org.jjj.user.controller;

import org.jjj.basic.utils.ResultAjax;
import org.jjj.user.dto.PhoneSmsLoginDTO;
import org.jjj.user.dto.RetrievepasswordDTO;
import org.jjj.user.dto.VerifyCodeDTO;
import org.jjj.user.service.IVerifyCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;
    //获取生成的图形验证码图片
    @GetMapping("/image/{key}")
    public ResultAjax getImgeCode(@PathVariable("key")String key){
        try {
            String base64ImageCode = verifyCodeService.getImgeCode(key);
            return ResultAjax.me().setResult(base64ImageCode);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("获取图形验证码失败!"+e.getMessage());
        }
    }

    /**
     * 账号注册获取短信验证码
     * @param dto
     * @return
     */
    @PostMapping("/sms/register")
    public ResultAjax getRegisterSmsCode(@RequestBody VerifyCodeDTO dto){
        try {
            return verifyCodeService.getRegisterSmsCode(dto);

        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("发送注册短信验证码失败！"+e.getMessage());
        }
    }
    /**
     * 微信登录绑定账号获取短信验证码
     * @param dto
     * @return
     */
    @PostMapping("/sms/bindWechat")
    public ResultAjax getBinderSmsCode(@RequestBody VerifyCodeDTO dto){
        try {
            return verifyCodeService.getBinderSmsCode(dto);

        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("发送绑定短信验证码失败！"+e.getMessage());
        }
    }

    /**
     * 手机验证码登录获取手机验证码
     * @param dto
     * @return
     */
    @PostMapping("/sms/verifyCodeLogin")
    public ResultAjax getPhoneSmsCode(@RequestBody PhoneSmsLoginDTO dto){
        try {
            return verifyCodeService.getPhoneSmsCode(dto);

        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("发送手机短信验证码失败！"+e.getMessage());
        }
    }
    @PostMapping("/sms/retrievepassword")
    public ResultAjax getRetrievepasswordSmsCode(@RequestBody RetrievepasswordDTO dto){
        try {
            return verifyCodeService.getRetrievepasswordSmsCode(dto);

        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("发送手机短信验证码失败！"+e.getMessage());
        }
    }
}
